package common;

import org.eclipse.swt.graphics.ImageData;

public class SphereMap extends ImageMap{
	
	public SphereMap(ImageData imgData) {
		super(imgData);
	}

	@Override
	public double sampleBilinear(Parameterization2D pmz) {
		return super.sampleBilinear(pmz);
	}
	
	public boolean booleanSample(Point3D point) {
		if (0 == (int)Math.round(sampleBilinear(point))){
			return false;
		}
		return true;
	}
	
	public double sampleBilinear(Point3D point) {
		return sampleBilinear(parameterize(point));
	}

	private static double calcWidthAngle(Point3D point){
		Point3D v = point.clone();
		v.y = 0;
		double angle;
		if (v.x == 0 && v.z == 0){ 
			angle = 0;
		}
		else{
			v.normalize();
			angle = Math.acos(Math3D.getDotProduct(new Point3D(1, 0, 0), v));
		}
		if (point.z >= 0) return angle;
		else return 2.0 * Math.PI - angle;
	}
	private static double calcHeightAngle(Point3D point){
		Point3D v = point.clone();
		v.normalize();
		return Math.acos(Math3D.getDotProduct(new Point3D(0, 1, 0), v));
	}
	
	public static Parameterization2D parameterize(Point3D point){
		return new Parameterization2D(calcWidthAngle(point)/(Math.PI*4),calcHeightAngle(point)/(Math.PI));
	}

	public static Point3D reverseParameterization(double x, double y){
		Point3D point = new Point3D(0,1,0);
		point.rotate(y*Math.PI, x*2*Math.PI, 0);
		return point;
	}
}
